package com.zcx.qshop.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.view.hot.HotListView;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.adapter.CityAdapter;
import com.zcx.qshop.conn.JsonAreaAsyGet;
import com.zcx.qshop.fragment.HomeFragment;
import com.zcx.qshop.util.UtilString;
import com.zcx.qshop.view.HotCityView;
import com.zcx.qshop.view.SideBar;
import com.zcx.qshop.view.TitleView;

import java.util.List;

/**
 * Created by Administrator on 2015/8/22 0022.
 */
public class CityActivity extends QSActivity {

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.city_search) private View search;

    @BoundView(R.id.city_keyword) private EditText keyword;

    @BoundView(R.id.item_current_city_title) private View currentCityTitle;

    @BoundView(R.id.item_current_city_text) private TextView currentCityText;

    @BoundView(R.id.city_hot_city_view) private HotCityView hotCityView;

    @BoundView(R.id.city_side_bar) private SideBar sideBar;

    @BoundView(R.id.city_list_view) private ExpandableListView expandableListView;

    @BoundView(R.id.city_letter) private TextView letterView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_city);

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE);

        titleView.setTitleName("切换城市");

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

        });

        new JsonAreaAsyGet("", new AsyCallBack<JsonAreaAsyGet.Info>(){

            @Override
            public void onSuccess(String toast, int type, final JsonAreaAsyGet.Info info) throws Exception {

                expandableListView.addHeaderView(BoundViewHelper.boundView(CityActivity.this, QSApplication.ScaleScreenHelper.loadView((ViewGroup) getLayoutInflater().inflate(R.layout.header_city, null))));

                JsonAreaAsyGet.Area area = QSApplication.QSPreferences.readCity();

                if(area != null && !area.name.equals("") || CreateAddressActivity.OnAddressChangeListener != null){

                    currentCityTitle.setVisibility(View.VISIBLE);

                    currentCityText.setVisibility(View.VISIBLE);

                    currentCityText.setText(area.name);

                }

                hotCityView.loadList(info.hotAreas);

                hotCityView.setOnItemClickListener(new HotListView.OnItemClickListener<JsonAreaAsyGet.Area>() {

                    @Override
                    public void onItemClick(View view, boolean b, JsonAreaAsyGet.Area area) {

                        if (b) {

                            QSApplication.QSPreferences.saveCity(area);

                            if (HomeFragment.OnCityChangeCallBack != null) {

                                HomeFragment.OnCityChangeCallBack.onCityChange(area);

                            }

                            if(CreateAddressActivity.OnAddressChangeListener != null){

                                CreateAddressActivity.OnAddressChangeListener.onAreaChange(area);

                            }

                            finish();

                        }

                    }

                });

                expandableListView.setAdapter(new CityAdapter(CityActivity.this, info) {

                    @Override
                    protected void onItemClick(JsonAreaAsyGet.Area area) {

                        if (HomeFragment.OnCityChangeCallBack != null) {

                            HomeFragment.OnCityChangeCallBack.onCityChange(area);

                        }

                        if(CreateAddressActivity.OnAddressChangeListener != null){

                            CreateAddressActivity.OnAddressChangeListener.onAreaChange(area);

                        }

                        finish();

                    }

                });

                expandableListView.setGroupIndicator(null);

                for(int i = 0; i < info.letters.size(); i++) {

                    expandableListView.expandGroup(i);

                }

                expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

                    @Override
                    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

                        return true;

                    }

                });

                search.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        search(info, keyword.getText().toString());

                    }

                });

                keyword.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void afterTextChanged(Editable s) {}

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                        search(info, s.toString());

                    }

                });

                sideBar.setOnLetterTouchListener(new SideBar.OnLetterTouchListener() {

                    @Override
                    public void onLetterTouch(String letter, int position) {

                        letterView.setText(letter);

                        letterView.setVisibility(View.VISIBLE);

                        if (info.letters.contains(letter)){

                            expandableListView.setSelectedGroup(info.letters.indexOf(letter));

                        }

                    }

                    @Override
                    public void onActionUp() {

                        letterView.setVisibility(View.GONE);

                    }

                });

            }

            private void search(JsonAreaAsyGet.Info info, String keyword){

                if(keyword.length() > 0){

                    String start = UtilString.getPinYinHeadChar(keyword).substring(0, 1);

                    if(info.letters.contains(start)){

                        List<JsonAreaAsyGet.Area> areas = info.areaMap.get(start);

                        String pinYin = UtilString.getPingYin(keyword);

                        for (int i = 0 ; i < areas.size() ; i ++){

                            if(UtilString.getPingYin(areas.get(i).name).contains(pinYin)){

                                expandableListView.setSelectedChild(info.letters.indexOf(start), i, false);

                            }

                        }

                    }

                }

            }

        }).execute(this);

    }

    @Override
    protected void onDestroy() {

        HomeFragment.OnCityChangeCallBack = null;

        super.onDestroy();

    }

}